home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / forth / amiga / amigaker.arc / files.txt < prev    next >
File List  |  1987-12-30  |  3KB  |  124 lines

  1. \ files.examples                                         23may87pja
  2.  
  3. \ Some (loadable) examples of file handling
  4.  
  5. \ set the environment
  6.  
  7. decimal
  8. only forth also definitions
  9.  
  10. \ Showing a file on the screen.
  11. \ Usage: typef <name>
  12.  
  13. : typef  \ name 
  14.    from
  15.    cr
  16.    begin  stdbuffer 250  [ dos ]  gets  dup
  17.    while  type  repeat
  18.    2drop  close-files ;
  19.  
  20.    \ The standard output buffer can be used as scratch buffer
  21.    \ because normally it is used for emit and multiple emits
  22.    \ of the same character.
  23.    \ gets requires a buffer address and a length, fills it with
  24.    \ a line and returns the length, zero it the end of the file
  25.    \ is reached.
  26.    \ Closing the file causes no harm, if only the 'from' file
  27.    \ should be closed, use: 'in-file file-close'
  28.  
  29.  
  30. \ Linecount, count the number of lines in a file.
  31. \ Usage: lc <name> |.|
  32.  
  33. : lc  \ name  (s -- n )    returns number of lines in a file.
  34.    from
  35.    0  begin  stdbuffer 250 [ dos ]  gets  nip
  36.       while  1+  repeat
  37.    in-file  file-close ;
  38.  
  39.    \ Putting the 'variable count' on the stack is simple, but the stack
  40.    \ must be tightly controlled. By getting rid of the buffer address and
  41.    \ then testing for null, the stack is emptied except for the count.
  42.    \ After the routine is finished, the count can be shown.
  43.  
  44.  
  45. \ Show a file with linecount
  46. \ Usage: typefn <name>
  47.  
  48. : typefn   \ name
  49.    from
  50.    cr
  51.    0  begin  stdbuffer 4+  250  [ dos ]  gets  dup
  52.       while  rot  1+
  53.              dup 4 .r  -rot
  54.              ascii : emit space
  55.              type
  56.       repeat
  57.    2drop drop  close-files ;
  58.  
  59.    \ The buffer must be moved up slightly in order for the emit of the
  60.    \ colon not to interfere with the line from the file.
  61.    \ The word space also uses the stdbuffer.
  62.    \ Printing a number doesn't interfere, it uses 'pad' which floats
  63.    \ above 'here'.
  64.  
  65.  
  66. \ Showing a file, using getc
  67. \ Usage: showc <name>
  68.  
  69. : showc   \ name
  70.    from
  71.    cr
  72.    begin  [ dos ]  getc dup 1+ 0<> while  emit  repeat
  73.    drop  close-files ;
  74.  
  75.  
  76. \ Counting characters, using getc
  77. \ Usage: countc <name>
  78.  
  79. : countc   \ name
  80.    from
  81.    cr
  82.    0
  83.    begin  [ dos ]  getc   1+ 0<>
  84.    while  1+  repeat
  85.    close-files . ;
  86.  
  87.  
  88. \ Counting lines, using getc
  89.  
  90. : countl   \ name
  91.    from
  92.    cr
  93.    0
  94.    begin [ dos ]  getc  dup 1+ 0<>
  95.    while  10 = if 1+ then
  96.    repeat
  97.    drop close-files . ;
  98.  
  99.  
  100. \ Typing only the lines with a character in it
  101.  
  102. : selectlines  \ name  (s char -- )
  103.    from
  104.    cr
  105.    begin [ dos ]  stdbuffer 4+ 250  gets  dup
  106.    while  3dup rot  scan  nip  if  type  else  2drop  then
  107.    repeat
  108.    2drop drop close-files ;
  109.  
  110.  
  111. \ Showing only the lines in a file starting with the character *
  112. \ Usefull in seeing the definitions in a file
  113.  
  114. : words-in  \ name
  115.    from  cr
  116.    begin  stdbuffer 4+ 250  [ dos ] gets  dup
  117.    while   over c@  ascii * =  if  type  else  2drop  then
  118.            key? if  key  control c = if exit then
  119.                     key  control c = if exit then  then
  120.    repeat
  121.    2drop  close-files  ;
  122.  
  123.  
  124.